package frame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import dao.CheckLogin;

import static java.lang.System.exit;

public class LoginFrame extends JFrame {

    //定义"帐号"和"密码"的标签
    private JLabel acc = new JLabel("帐号： ");
    private JLabel pass = new JLabel("密码： ");

    //定义存放用户帐号和密码的文本项
    private JTextField accText = new JTextField();
    private JPasswordField passText = new JPasswordField();

    //定义登录界面的Box容器，以便使用BoxLayout布局器
    private Box up = Box.createHorizontalBox();
    private Box center = Box.createHorizontalBox();
    private Box center1 = Box.createHorizontalBox();
    private Box upCenter = Box.createVerticalBox();
    private Box down = Box.createHorizontalBox();
    //定义登录按钮
    private JButton login = new JButton("登录");
    private JButton cancle = new JButton("取消");
    private JButton reg = new JButton("注册");
    JLabel role = new JLabel("角色：");
    JComboBox identity = new JComboBox();

    public LoginFrame() {
        super("学生成绩管理系统");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //布局各容器，并设置各容器的水平和垂直间距
        up.add(Box.createHorizontalStrut(50));
        up.add(acc);
        up.add(Box.createHorizontalStrut(10));
        up.add(accText);
        up.add(Box.createHorizontalStrut(100));

        center.add(Box.createHorizontalStrut(50));
        center.add(pass);
        center.add(Box.createHorizontalStrut(10));
        center.add(passText);
        center.add(Box.createHorizontalStrut(100));

        center1.add(Box.createHorizontalStrut(50));
        center1.add(role);
        identity.addItem(new String("学生"));
        identity.addItem(new String("教师"));
        identity.addItem(new String("管理员"));
        center1.add(Box.createHorizontalStrut(10));
        center1.add(identity);
        center1.add(Box.createHorizontalStrut(100));

        upCenter.add(Box.createVerticalStrut(20));
        upCenter.add(up);
        upCenter.add(Box.createVerticalStrut(20));
        upCenter.add(center);
        upCenter.add(Box.createVerticalStrut(20));
        upCenter.add(center1);
        upCenter.add(Box.createVerticalStrut(20));

        down.add(Box.createHorizontalStrut(270));
        down.add(login, BorderLayout.EAST);
        down.add(cancle, BorderLayout.WEST);
        down.add(reg, BorderLayout.WEST);
        down.add(Box.createHorizontalStrut(30));
        down.add(Box.createVerticalStrut(50));



        this.add(upCenter, BorderLayout.CENTER);
        this.add(down, BorderLayout.SOUTH);

        this.setBounds(550, 250, 350, 200);

        this.pack();
        this.setVisible(true);
        initListeners();
    }

    public void initListeners() {

        this.reg.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                reg();
            }

        });
        this.login.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                login();
            }

        });

        this.cancle.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                cancle();
            }

        });
    }

    public void login() {
        String name = this.accText.getText().trim();
        char[] passes = this.passText.getPassword();
        StringBuffer password = new StringBuffer();
        for (char c : passes) {
            password.append(c);
        }
        String sf = (String) identity.getSelectedItem();//身份
        if (sf.equals("管理员")) {
            CheckLogin checkLogin = new CheckLogin();
            if (checkLogin.adminCheckLogin(name, password.toString())) {
                JOptionPane.showMessageDialog(center, "欢迎来到学生管理系统", "登录成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
                AdminMainFrame adminMainFrame = new AdminMainFrame();
            } else {
                JOptionPane.showMessageDialog(center, "登录失败", "error", JOptionPane.ERROR_MESSAGE);
            }
        }
        if (sf.equals("学生")) {
            CheckLogin checkLogin = new CheckLogin();
            if (checkLogin.studentCheckLogin(name, password.toString())) {
                JOptionPane.showMessageDialog(center, "欢迎来到学生管理系统", "登录成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
                String sno = accText.getText();
                StudentMainFrame studentMainFrame = new StudentMainFrame(sno);
            } else {
                JOptionPane.showMessageDialog(center, "登录失败，账号或密码错误", "error", JOptionPane.ERROR_MESSAGE);
            }
        }
        if (sf.equals("教师")) {
            CheckLogin checkLogin = new CheckLogin();
            if (checkLogin.teacherCheckLogin(name, password.toString())) {
                JOptionPane.showMessageDialog(center, "欢迎登录学生管理系统", "登录成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
                TeacherMainFrame teacherMainFrame = new TeacherMainFrame();
            } else {
                JOptionPane.showMessageDialog(center, "登录失败，账号或密码错误", "error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    public void reg(){
        dispose();
        new RegistView();
    }

    public void cancle() {
        JOptionPane.showMessageDialog(center, "已取消", "success", JOptionPane.INFORMATION_MESSAGE);
        exit(0);
    }


    public static void main(String[] args) {
        LoginFrame loginFrame = new LoginFrame();

    }
}
